﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtyManagementSystem.BLL;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.Web.RealtyManagers
{
    public partial class AddParking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDdlBuildings();
                BindHouse();
                BindGridView();
            }
        }

        private void BindHouse()
        {
            var bId = Convert.ToInt16(ddlBuilding.SelectedValue);
            using (var context = new realtydbEntities())
            {
                var hes = (from hs in context.Households
                           where hs.BuildingId == bId
                           select new
                           {
                               householdID = hs.HouseholdID,
                               name = hs.aspnet_Users.UserName
                           }).ToList();
                this.ddlHousehold.DataSource = hes;
                this.ddlHousehold.DataValueField = "householdID";
                this.ddlHousehold.DataTextField = "name";
                this.ddlHousehold.DataBind();
            }
        }

        private void BindGridView()
        {
            var bId = Convert.ToInt16(ddlBuilding.SelectedValue);
            using (var context = new realtydbEntities())
            {
                var parkings = (from p in context.Parkings.Include("Household.aspnet_Users").Include("Building")
                                where p.BuildingId == bId
                                select p).ToList();

                this.gvParkings.DataSource = parkings;
                this.gvParkings.DataBind();
            }
        }

        private void BindDdlBuildings()
        {
            var buildings = new BuildingsProvider();
            ddlBuilding.DataSource = buildings.GetBuildings();
            ddlBuilding.DataValueField = "BuildingId";
            ddlBuilding.DataTextField = "BuildingName";
            ddlBuilding.DataBind();
            this.ddlBuilding.SelectedIndex = 0;
        }

        protected void ddlBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
            BindHouse();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var hasEmpty = false;
            var bId = Convert.ToInt16(ddlBuilding.SelectedValue);
            using (var conetxt = new realtydbEntities())
            {
                var building = (from bd in conetxt.Buildings.Include("Parkings.Household")
                                where bd.BuildingId == bId
                                select bd).SingleOrDefault();
                foreach (var pking in building.Parkings)
                {
                    if (pking.Household == null)
                    {
                        var hId = new Guid(ddlHousehold.SelectedValue);
                        pking.HouseholdID = hId;
                        hasEmpty = true;
                        conetxt.SaveChanges();
                        break;
                    }
                }
            }
            BindGridView();

            if (!hasEmpty)
            {
                lblMsg.Text = "车位已满！";
            }
            else
            {
                lblMsg.Text = "分配成功！";
            }
        }
    }//class
}